package com.wanpinghui.supplier.product.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.widgets.PlusMinuesView;
import com.wanpinghui.wphlibrary.model.product.bean.ProductBean;
import com.wanpinghui.wphlibrary.model.product.bean.ProductFormatBean;
import com.yunshl.yunshllibrary.recyclerview.BaseRecyclerViewAdapter;
import com.yunshl.yunshllibrary.utils.NumberUtil;

import java.util.ArrayList;
import java.util.List;

/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2017/12/20
 ***/

public class ProductListAdapter extends BaseRecyclerViewAdapter<ProductBean, ProductListAdapter.ProductHolder> {
    private OnButtonClickListener listener;

    public ProductListAdapter(Context context) {
        super(context);
    }

    private ProductBean currentProduct;

    public ProductBean getData(int position) {
        if (getItemCount() > position) {
            return datas.get(position);
        }
        return null;
    }

    @Override
    public void setDatas(List<ProductBean> datas) {
        if (datas != null && datas.size() > 0) {
            for (ProductBean bean : datas) {
                if (bean.getFormatList() != null && bean.getFormatList().size() > 0) {
                    for (ProductFormatBean productFormatBean : bean.getFormatList()) {
                        productFormatBean.setCount(productFormatBean.getCart_item_quantity());
                    }
                } else {
                    bean.setSelectCount(bean.getCart_item_quantity());
                }
            }
        }
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public void addDatas(List<ProductBean> dataList) {
        if (dataList != null && dataList.size() > 0) {
            for (ProductBean bean : dataList) {
                if (bean.getFormatList() != null && bean.getFormatList().size() > 0) {
                    for (ProductFormatBean productFormatBean : bean.getFormatList()) {
                        productFormatBean.setCount(productFormatBean.getCart_item_quantity());
                    }
                } else {
                    bean.setSelectCount(bean.getCart_item_quantity());
                }
            }
            if (this.datas == null) {
                this.datas = new ArrayList<>();
            }
            this.datas.addAll(dataList);
            notifyDataSetChanged();
        }
    }

    public void setButtonClickListener(OnButtonClickListener listener) {
        this.listener = listener;
    }

    @Override
    public ProductHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_recv_product_list, null);
        return new ProductHolder(view);
    }

    @Override
    public void onBindViewHolder(final ProductHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        final ProductBean bean = datas.get(position);
        Glide.with(context).load(bean.getMainImage()).asBitmap().placeholder(R.mipmap.goods_bg_loading_a).error(R.mipmap.goods_bg_loading).into(holder.imageViewImage);
        holder.textViewName.setText(bean.getName());
        holder.textViewSecondName.setText(bean.getSecond_name());
        if (bean.isIs_consult()) {
            holder.textViewPrice.setText("议价");
        } else {
            if (bean.getMax_price() == bean.getMin_price() || bean.getMax_price() < bean.getMin_price()) {
                String priceStr = "￥"+NumberUtil.double2String( bean.getMin_price());
                SpannableString priceSpanStr = new SpannableString(priceStr);
                if (priceStr.indexOf(".") >= 0) {
                    priceSpanStr.setSpan(new RelativeSizeSpan(0.77f), priceStr.indexOf("."), priceStr.indexOf(".") + 3, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                }
                holder.textViewPrice.setText(priceSpanStr);
            } else {
                String priceStr = "￥" + NumberUtil.double2String(bean.getMin_price()) + "-" + NumberUtil.double2String(bean.getMax_price());
                SpannableString priceSpanStr = new SpannableString(priceStr);
                if (priceStr.indexOf(".") >= 0) {
                    priceSpanStr.setSpan(new RelativeSizeSpan(0.77f), priceStr.indexOf("."), priceStr.indexOf(".") + 3, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                }
                if (priceStr.lastIndexOf(".") != priceStr.indexOf(".")) {
                    priceSpanStr.setSpan(new RelativeSizeSpan(0.77f), priceStr.lastIndexOf("."), priceStr.lastIndexOf(".") + 3, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                }
                holder.textViewPrice.setText(priceSpanStr);
            }
        }
        if (bean.getFormatCount() > 1) {
            holder.mLayoutOneFormat.setVisibility(View.GONE);
            holder.mLayoutMoreFormat.setVisibility(View.VISIBLE);
            if (bean.getCart_item_quantity() > 0) {
                holder.textViewCount.setText(Integer.toString(bean.getCart_item_quantity()));
            } else {
                holder.textViewCount.setText("");
            }
            holder.textViewSelectFormat.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onSelectFormat(bean);
                    }
                }
            });
        } else if (bean.getFormatCount() == 0) {
            holder.mLayoutOneFormat.setVisibility(View.VISIBLE);
            holder.mLayoutMoreFormat.setVisibility(View.GONE);
            holder.plusMinuesView.setCount(bean.getCount());

            holder.plusMinuesView.setOnCountChangeListener(new PlusMinuesView.OnCountChangeListener() {
                @Override
                public void onPlus() {
                    if (listener != null) {
                        listener.onAddCount(holder.plusMinuesView, 1);
                    }
                }

                @Override
                public void onMinues() {
                }

                @Override
                public void onCountChange(int count) {
                    bean.setSelectCount(count);
                    currentProduct = bean;
                    mHandler.removeMessages(1);
                    mHandler.sendEmptyMessageDelayed(1, 400);
//                    if (listener != null) {
//                        listener.onSelect(bean,null);
//                    }
                }
            });

        } else {
            final ProductFormatBean formatBean = bean.getFormatList().get(0);
            holder.mLayoutOneFormat.setVisibility(View.VISIBLE);
            holder.mLayoutMoreFormat.setVisibility(View.GONE);
            holder.plusMinuesView.setCount(formatBean.getCount());
            if (bean.getFormatCount() == 1) {
                holder.plusMinuesView.setOnCountChangeListener(new PlusMinuesView.OnCountChangeListener() {
                    @Override
                    public void onPlus() {
                        if (listener != null) {
                            listener.onAddCount(holder.plusMinuesView, 1);
                        }
                    }

                    @Override
                    public void onMinues() {
                    }

                    @Override
                    public void onCountChange(int count) {
                        formatBean.setCount(count);
                        currentProduct = bean;
                        mHandler.removeMessages(1);
                        mHandler.sendEmptyMessageDelayed(1, 400);
//                        if (listener != null) {
//                            listener.onSelect(bean,null);
//                        }
                    }
                });
            } else {
                holder.mLayoutMoreFormat.setVisibility(View.GONE);
                holder.mLayoutOneFormat.setVisibility(View.GONE);
            }
        }
    }

//    private void setEdittextHideShow(ProductFormatBean bean, ProductHolder holder) {
//        if (bean.getCount() > 0) {
//            holder.textViewMinues.setVisibility(View.VISIBLE);
//            holder.editTextCount.setVisibility(View.VISIBLE);
//        }else {
//            holder.textViewMinues.setVisibility(View.GONE);
//            holder.editTextCount.setVisibility(View.GONE);
//        }
//    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    class ProductHolder extends RecyclerView.ViewHolder {
        ImageView imageViewImage;
        TextView textViewName;
        TextView textViewSecondName;
        TextView textViewMinues;
        PlusMinuesView plusMinuesView;
        TextView textViewCount;
        TextView textViewSelectFormat;
        FrameLayout mLayoutOneFormat;
        LinearLayout mLayoutMoreFormat;
        TextView textViewPrice;

        public ProductHolder(View itemView) {
            super(itemView);
            imageViewImage = (ImageView) itemView.findViewById(R.id.iv_image);
            textViewName = (TextView) itemView.findViewById(R.id.tv_name);
            textViewSecondName = (TextView) itemView.findViewById(R.id.tv_second_name);
            plusMinuesView = (PlusMinuesView) itemView.findViewById(R.id.view_plus_minues);
            textViewMinues = (TextView) itemView.findViewById(R.id.tv_minues);
            textViewCount = (TextView) itemView.findViewById(R.id.tv_count);
            textViewSelectFormat = (TextView) itemView.findViewById(R.id.tv_select_format);
            mLayoutMoreFormat = (LinearLayout) itemView.findViewById(R.id.ll_more_format);
            mLayoutOneFormat = (FrameLayout) itemView.findViewById(R.id.fl_one_format);
            textViewPrice = (TextView) itemView.findViewById(R.id.tv_price);
            itemView.setLayoutParams(new GridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        }
    }

    public interface OnButtonClickListener {
        void onSelectFormat(ProductBean bean);

        void onAddCount(View view, int count);

        void onSelect(ProductBean bean, ProductFormatBean productFormatBean);
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    if (listener != null && currentProduct != null) {
                        listener.onSelect(currentProduct, null);
                    }
                    currentProduct = null;
                    break;
            }
        }
    };
}
